<template>
	<view class="container">
		<view class="card-form">
			<view class="title">请绑定本人的银行卡</view>
			<u-form :model="form" ref="uForm" class="mt-30" label-width="150">
				<u-form-item label="持卡人" prop="name">
					<u-input v-model="form.name" placeholder="请输入您的姓名" />
				</u-form-item>
				<u-form-item prop="account" label="银行卡号">
					<view class="form-item-wrapper">
						<u-input v-model="form.account" placeholder="请输入银行卡号" />
					</view>
				</u-form-item>
				<u-form-item prop="bank" label="发卡银行">
					<view class="form-item-wrapper">
						<u-input v-model="form.bank" type="select" @click="bankShow = true" />
					</view>
				</u-form-item>
				<u-form-item prop="area" label="开户省市">
					<view class="form-item-wrapper">
						<u-input v-model="form.area" type="select" @click="areaShow = true" />
					</view>
				</u-form-item>
				<u-form-item prop="openArea" label="开户支行">
					<view class="form-item-wrapper ">
						<u-input v-model="form.openArea" placeholder="请输入开户支行" />
					</view>
				</u-form-item>
			</u-form>
			<view class="common-btn submit-btn" @tap="submit">确认提交</view>
			<u-picker v-model="bankShow" mode="selector" :range="bankList" @confirm="bankConfirm"></u-picker>
			<u-picker v-model="areaShow" mode="region" @confirm="areaConfirm"></u-picker>
		</view>
	</view>
</template>

<script>
	import {
		bankList
	} from "@/common/config.js"
	export default {
		data() {
			return {
				form: {
					name: "",
					account: "",
					bank: "",
					area: "",
					openArea: ""
				},
				rules: {
					name: {
						required: true,
						message: '请输入真实姓名',
						trigger: ['change', 'blur'],
					},
					account: {
						required: true,
						message: '请输入银行卡号',
						trigger: ['change', 'blur'],
					},
					bank: {
						required: true,
						message: '请输入发卡银行',
						trigger: ['change', 'blur'],
					},
					area: {
						required: true,
						message: '请输入开户省市',
						trigger: ['change', 'blur'],
					},
					openArea: {
						required: true,
						message: '请输入开户支行',
						trigger: ['change', 'blur'],
					}
				},
				bankShow: false,
				bankList: bankList,
				areaShow: false
			};
		},
		methods: {
			bankConfirm(value) {
				this.form.bank=this.bankList[value[0]]
			},
			areaConfirm(value) {
				this.form.area=`${value.province.label}-${value.city.label}-${value.area.label}`
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						let form={
							address:this.form.area,
							bank_child:this.form.openArea,
							bank_code:this.form.account,
							bank_name:this.form.bank,
							userName:this.form.name
						}
						this.$u.api.bindBankCardApi(form).then(()=> {
							uni.showToast({
								icon:"success",
								title:"操作成功"
							})
							setTimeout(()=>{
								this.$Router.back(1)
							},1000)
						})
					} else {
						console.log('验证失败');
					}
				});
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
	}
</script>

<style lang="scss">
	.container {
		.card-form{
			padding: 30rpx;
			background-color: #FFFFFF;
			.title{
				font-size: 24rpx;
				color: #999;
			}
		}
		.form-item-wrapper{
			flex:1;
		}
		
	}
</style>
